package com.goods.controller.business;

import com.goods.business.service.ProductCategoryService;
import com.goods.common.model.business.ProductCategory;
import com.goods.common.response.ResponseBean;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import com.goods.common.vo.business.ProductCategoryVO;
import com.goods.common.vo.system.PageVO;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/business/productCategory")
public class productCategoryController {


    @Autowired
    private ProductCategoryService productCategoryService;


    /**
     * 获取所有 类别列表展示
     * @param pageNum  起始页
     * @param pageSize 每页显示条数
     * @return
     */
    @GetMapping("categoryTree")
    public ResponseBean categoryTree(@RequestParam("pageNum") int pageNum,
                                     @RequestParam("pageSize") int pageSize){

        //调用参数，获取分类信息
        PageVO<ProductCategoryTreeNodeVO> pageVO = productCategoryService.categoryTree(pageNum, pageSize);
        return ResponseBean.success(pageVO);
    }

    /**
     * 获取添加时的分类数据
     * @return
     */
    @GetMapping("/getParentCategoryTree")
    public ResponseBean getParentCategoryTree(){
        List<ProductCategoryTreeNodeVO> data =  productCategoryService.getParentCategoryTree();
        return ResponseBean.success(data);
    }

    /**
     * 添加分类数据
     */
    @PostMapping("/add")
    public ResponseBean add(@RequestBody ProductCategory productCategory){
        try {
            if (productCategory != null){
                productCategoryService.add(productCategory);
            }
            return ResponseBean.success();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseBean.error("添加失败");
    }


    /**
     * 回显分类
     * @param id
     * @return
     */
    @GetMapping("/edit/{id}")
    public ResponseBean edit(@PathVariable Long id){
        try {
            ProductCategoryVO edit = productCategoryService.edit(id);
            return ResponseBean.success(edit);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseBean.error("编辑失败");
        }
    }

    /**
     * 保存编辑分类
     * @param productCategory
     */
    @PutMapping("/update/{id}")
    public ResponseBean update(@PathVariable Long id ,@RequestBody ProductCategory productCategory){
        try {
            if (productCategory != null){
                productCategoryService.update(productCategory);
            }
            return ResponseBean.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseBean.error("编辑失败");
        }
    }

    @DeleteMapping("delete/{id}")
    public ResponseBean delete(@PathVariable Long id){
        try {
            productCategoryService.delete(id);
            return ResponseBean.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseBean.error("删除失败");
        }
    }

}
